package treeset02;


import java.util.Comparator;
import java.util.TreeSet;

/**
 * 利用比较器来实现,原理和自然排序规则类似
 */
public class TreeSetDemo04 {
    public static void main(String[] args) {

        TreeSet<Person> ts = new TreeSet<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getAge() == o2.getAge() ? o1.getName().compareTo(o2.getName()) : o1.getAge() - o2.getAge();
            }
        });
        Person p1 = new Person("xiaoxin", 18);
        Person p2 = new Person("xiaotianin", 18);//年龄相同
        Person p3 = new Person("xiaowang", 20);

        ts.add(p1);
        ts.add(p2);
        ts.add(p3);

        System.out.println(ts);
    }
}
